package com.proton.carepatchtemp.utils;

import com.apkfuns.logutils.LogUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Modifier;
import java.lang.reflect.Type;

//gson解析
public class GsonUtils {
    public static final String TAG = "GsonUtils";

    public static Gson getIntGson() {
        return new GsonBuilder()
                .
                        registerTypeAdapter(Double.class, new JsonSerializer<Double>() {

                            @Override
                            public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
                                LogUtils.e(TAG, src);
                                if (src == src.longValue())
                                    return new JsonPrimitive(src.longValue());
                                return new JsonPrimitive(src);
                            }
                        })
                .
                        registerTypeAdapter(Double.class, new JsonDeserializer<Double>() {

                            @Override
                            public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                                LogUtils.e(TAG, json);
                                return 12d;
                            }
//
//                           @Override
//                           public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
//                               LogMuch.e(src);
//                               if (src == src.longValue())
//                                   return new JsonPrimitive(src.longValue());
//                               return new JsonPrimitive(src);
//                           }
                        })
                .create();
    }

    public static Gson getMapIntGson() {
        Gson gson = new Gson();
//        Gson gson=new GsonBuilder().registerTypeAdapter(HashMap.class, new JsonDeserializer<HashMap>() {
//            public HashMap deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
//                    throws JsonParseException {
//                def resultMap=[:]
//                def jsonObject = json.getAsJsonObject();
//                Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet()
//                for (Map.Entry<String, JsonElement> entry : entrySet) {
//                    resultMap[entry.getKey()] = entry.getValue()
//                }
//                return resultMap
//            }
//        }).create();
        return gson;
    }

    public static Gson getGson() {
        return new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss")
                .excludeFieldsWithModifiers(Modifier.PROTECTED)
                .create();
    }

    public static Gson getGsonX() {
        return new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss")
                .excludeFieldsWithoutExposeAnnotation()
                .create();
    }

    public static Gson getGsonXX() {
        return new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss")
                .excludeFieldsWithoutExposeAnnotation()
                .serializeSpecialFloatingPointValues()
                .create();
    }

    public static Gson getGson(String format) {
        return new GsonBuilder().serializeNulls().setDateFormat(format)
                .excludeFieldsWithModifiers(Modifier.PROTECTED)
                .create();
    }

// --Commented out by Inspection START (2016/3/15 11:13):
//    public static Gson getGson(String format,String a){
//        return new GsonBuilder().serializeNulls().setDateFormat(format)
//                .excludeFieldsWithModifiers(Modifier.PROTECTED)
//                .create();
//    }
// --Commented out by Inspection STOP (2016/3/15 11:13)
}
